今天我們要介紹 MapKit 手機地圖我們需要先在Info 裡面加入確認請求位子的請求
加入完後
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
centerMapOnLocation(location: location)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while updating location: \(error.localizedDescription)")
}
}
透過上面程式就可以獲取用戶的地理位子